﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LL.Business;
using NanoERP.Business;
using NanoERP.Model.Poco;
using NanoERP.Model.SystemMgmt;
using Omu.ValueInjecter;
//using NanoERP.Data;

namespace NanoERP.Business.Permission
{
    //用户表逻辑
    public class UserService : ServiceBase
    {

        #region 基础方法

        public SYS_User Get(Guid guid)
        {
            var dao = this.DbSession;
            return dao.Get<SYS_User>(guid);
        }

        public void Add(SYS_User user)
        {
            var dao = this.DbSession;
            dao.Insert(user);
        }

        public void Delete(Guid guid)
        {
            var dao = this.DbSession;
            dao.Delete<SYS_User>(guid);
        }

        public void Update(SYS_User user)
        {
            var dao = this.DbSession;
            dao.Update(user);
        }

        public void MoveToDepartment(Guid userId, Guid? departmentId)
        {
            var dao = this.DbSession;
            var user = dao.Get<SYS_User>(userId);
            user.DepartmentId = departmentId;
            dao.Update(user);
        }

        public void SetRoles(Guid userId, IList<Guid> roles)
        {
            var sqlHelper = this.DbSession;
            sqlHelper.ExcuteNonQuery("Delete From SYS_Roles_Users Where UserId=@0", userId);
            foreach (var roleId in roles)
            {
                sqlHelper.ExcuteNonQuery("Insert Into SYS_Roles_Users(UserId,RoleId) Values(@0,@1)", userId, roleId);
            }
        }

        public void SetRights(Guid userId, IList<Guid> rights)
        {
            var sqlHelper = this.DbSession;
            sqlHelper.ExcuteNonQuery("Delete from Rights_User where UserId=@0", userId);
            foreach (var rightId in rights)
            {
                sqlHelper.ExcuteNonQuery("insert into Rights_User(UserId,RightId) VALUES(@0,@1)", userId, rightId);
            }
        }

        #endregion

        /// <summary>
        /// 获得整个部门/人员树
        /// </summary>
        /// <returns></returns>
        public IList<DepartmentTree> GetDepTreeWithUsers()
        {
            using (var session = BusinessSessionFactory.NewSession())
            {
                //取出部门树
                var deps = session.Get<DepartmentService>()
                    .GetDepartmentTree();
                //为部门树填充人员
                session.Get<UserService>()
                    .FillUsersForDepartment(deps, true);
                //为人员填充角色
                //session.Get<RoleService>()
                //    .FillRolesForDepartmentUsers(deps, true);
                return deps;
            }
        }

        public IList<UserModel> GetUsersFromDepartment(Guid departmentId)
        {
            var userList = this.DbSession
                       .Query<SYS_User>("DepartmentId=@0", departmentId);
            var userModelList = new List<UserModel>();
            foreach (var item in userList)
            {
                var userModel = new UserModel();
                userModel.InjectFrom(item);
                userModelList.Add(userModel);
            }
            return userModelList;
        }

        public void FillUsersForDepartment(IList<DepartmentTree> departments, bool recursion = false)
        {
            foreach (var department in departments)
            {
                FillUsersForDepartment(department, recursion);
                FillUsersForDepartment(department.Children, recursion);
            }
        }

        public void FillUsersForDepartment(DepartmentTree department, bool recursion = false)
        {
            var userList = GetUsersFromDepartment(department.Id);

            foreach (var user in userList)
            {
                department.Users.Add(user);
            }
        }


        public SYS_User GetUser(string userName, string password)
        {
            return DbSession
                .Query<SYS_User>("LoginName=@0 AND Password=@1", userName, password)
                .FirstOrDefault();
        }
    }
}
